package com.appspot.battlerafts.servlets;

import com.appspot.battlerafts.utils.Pusher;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.appspot.battlerafts.utils.Pusher.hmacsha256Representation;

/**
 * Created with IntelliJ IDEA.
 * User: Tonis
 * Date: 07.04.12
 * Time: 17:56
 * To change this template use File | Settings | File Templates.
 */
public class PusherAuthenticate extends HttpServlet {
    private static final Logger log = Logger.getLogger(PusherAuthenticate.class.getName());

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String channel_name = request.getParameter("channel_name");
        String socket_id = request.getParameter("socket_id");
        log.info(channel_name);
        log.info(socket_id);
        log.setLevel(Level.INFO);

        String string_to_sign = socket_id + ":" + channel_name;
        String signature = hmacsha256Representation(string_to_sign);

        PrintWriter writer = response.getWriter();
        writer.write("{\"auth\":\""+ Pusher.pusherApplicationKey+":"+signature +"\"}");
        return;
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
